package com.example.blog.controller;

import com.example.blog.common.PasswordUtils;
import com.example.blog.entity.Blog;
import com.example.blog.entity.User;
import com.example.blog.entity.vo.UserVo;
import com.example.blog.service.BlogService;
import com.example.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@RequestMapping("/user")
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private BlogService blogService;

    @PostMapping("/login")
    public String login(String username,String password,HttpServletRequest req) {
        if (password == null || password.equals("") || username == null || username.equals("")) {
            return "redirect:/login.html";
        }
        // 先查看用户名是否存在
        User user = userService.selectByUserName(username);
        if (user == null || !PasswordUtils.check(password,user.getPassword())) {
            return "redirect:/login.html";
        }
        HttpSession session = req.getSession();
        session.setAttribute("user",user);
        return "redirect:/blog_list.html";
    }

    @GetMapping("/loginer")
    @ResponseBody
    public UserVo isLogin(Integer blogid,HttpServletRequest req) {
        HttpSession session = req.getSession(false);
        User user = (User) session.getAttribute("user");
        // 如果blogid 为空表示 是获取主页的文章数 否则是获取别人的文章数
        if (blogid == null) {
            UserVo userVo = new UserVo();
            userVo.setUserid(user.getUserid());
            userVo.setPassword(user.getPassword());
            userVo.setUsername(user.getUsername());
            userVo.setBlogCount(userService.getBlogNum(user.getUserid()));
            return userVo;
        }else {
            Blog blog = blogService.selectById(blogid);
            // 查到写博客的那个人
            User user1 = userService.selectByUserId(blog.getUserid());
            UserVo userVo = new UserVo();
            userVo.setUserid(user1.getUserid());
            userVo.setPassword(user1.getPassword());
            userVo.setUsername(user1.getUsername());
            userVo.setBlogCount(userService.getBlogNum(user1.getUserid()));
            return userVo;
        }
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest req) {
        HttpSession session = req.getSession(false);
        session.removeAttribute("user");
        return "redirect:/login.html";
    }


    @RequestMapping("/reg")
    public String addUser(String username,String password) {
        if (username == null || username.equals("") || password == null || password.equals("")) return "redirect:/reg.html";
        // 如果没有将sql中的username字段设置为 唯一的 ,那么就需要判断如果该注册用户 的账号密码已经被被人使用 ,就不能成功注册
        if (userService.selectUser(username,password) != null)  return "redirect:/reg.html";
        userService.addUser(username, PasswordUtils.encrypt(password));
        return "redirect:/login.html";
    }
    @RequestMapping("/upuser")
    public String upUser(String username,String password) {
        if (userService.selectByUserName(username) == null) {
            return "redirect:/upworderror.html";
        }
        userService.upUser(username,PasswordUtils.encrypt(password));
        return "redirect:/login.html";
    }
}
